define(['jquery', 'bootstrap', 'backend', 'table', 'form'], function ($, undefined, Backend, Table, Form) {

    var Controller = {
        index: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'sharpshooter/index' + location.search,
                    add_url: 'sharpshooter/add',
                    edit_url: 'sharpshooter/edit',
                    del_url: 'sharpshooter/del',
                    multi_url: 'sharpshooter/multi',
                    import_url: 'sharpshooter/import',
                    table: 'sharpshooter',
                }
            });

            var table = $("#table");

            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'id',
                sortName: 'id',
                search:false,
                columns: [
                    [
                        {checkbox: true},
                        {field: 'id', title: __('Id')},
                        {
                            field: 'user.nickname',
                            title: __('User'),
                            operate: 'LIKE',
                            halign: "center",
                            searchable: false,
                            formatter: Table.api.formatter.content
                        },
                        {
                            field: 'club_dtl.name',
                            title: __('社团'),
                            operate: 'LIKE',
                            halign: "center",
                            searchable: false,
                            formatter: function(value, row, index) {
                                return value || '';
                            }
                        },
                        // {field: 'money', title: __('Money'), operate:'BETWEEN'},

                        {field: 'remark', title: __('Remark'), operate: 'LIKE', formatter: Table.api.formatter.flag},
                        {field: 'describe', title: __('Describe'), operate: 'LIKE', table: table, class: 'autocontent', formatter: Table.api.formatter.content},
                        {field: 'title', title: __('Title'), operate: 'LIKE', table: table, class: 'autocontent', formatter: Table.api.formatter.content},
                        {field: 'sort', title: __('Sort')},
                        {field: 'addtime', title: __('Addtime'), operate:'RANGE', addclass:'datetimerange', autocomplete:false},
                        {field: 'operate', title: __('Operate'), table: table, events: Table.api.events.operate, formatter: Table.api.formatter.operate}
                    ]
                ]
            });

            // 为表格绑定事件
            Table.api.bindevent(table);
        },
        add: function () {
            Controller.api.bindevent();
        },
        edit: function () {
            Controller.api.bindevent();
        },
        api: {
            bindevent: function () {
                Form.api.bindevent($("form[role=form]"));
                
                // 设置子社团下拉的动态参数
                $("#c-club_two_id").data("params", function () {
                    return {custom: {user_id: $("#c-user_id").val()}};
                });
                
                // 监听用户变化，清空子社团选择
                $("#c-user_id").on('change', function() {
                    $("#c-club_two_id").selectPageClear();
                });
            }
        }
    };
    return Controller;
});
